// 04jyhs/shoppingMallAdmin/AddClassify/AddClassify.js
var co = require("../../../resource/js/common.js")
var app = getApp()
var appData = app.Data
var _this
Page({
   data: {
      isShowInputLabelArr: [0, 0],
      goodsCateList: null,
      buttonText: '添加商品分类',
      pick_i: 0,
      edit_id: -1,
      mode: '',
      printerList: [],
      exchange_id: -1,
      sorting: '编辑排序',
      shopInfo: ''
   },
   // 进入页面执行
   onLoad: function (options) {
      _this = this
      // 获取商家自定义分类
      _this.getGoodsCateData()
      this.setData({
         shopInfo: app.Data.shopInfo
      })
   },
   // 编辑排序按钮
   sorting: function () {
      this.setData({
         mode: this.data.mode == '' ? 'sort' : '',
         sorting: this.data.mode == 'sort' ? '编辑排序' : '返回',
      })
   },
   // 添加商品按钮
   addButtonChange: function () {
         this.setData({
            pick_i: 0,
            mode: this.data.mode == '' ? 'add' : '',
            buttonText: this.data.mode == '' ? '取消添加' : '添加商品分类',
            edit_id: -1,
            isShowInputLabelArr: [0, 0]
         })
   },
   //商品分类排序
   toExchange: function (e) {
      var td = this.data
      var index = e.currentTarget.id
      if (td.exchange_id != -1 && index != '') {
         co.getMealApi('jyhs/Prdmg/swapBizCateSort', {
            Category_ID_1: td.goodsCateList[index].Category_ID,
            Category_ID_2: td.goodsCateList[td.exchange_id].Category_ID,
            Biz_ID: appData.shopInfo.Biz_ID,
         }, function (res) {
            if (res.code == 0) {
               _this.setData({
                  goodsCateList: res.data,
                  exchange_id: -1
               })
            }
         })
      } else {
         this.setData({
            exchange_id: index == '' ? -1 : index
         })
      }
   },
   //打开编辑
   editgoods: function (e) {
         var td = this.data
         var index = e.currentTarget.id
         var printer = td.printerList[index]
         this.setData({
            edit_id: td.edit_id == index ? -1 : index,
            isShowInputLabelArr: index > -1 ? [1, 1] : [0, 0],
            mode: '',
            buttonText: '添加商品分类'
         })
   },
   //添加或修改
   submitGoodsInfo: function (e) {
      var td = this.data
      var param = e.detail.value
      var url
      param.Biz_ID = appData.shopInfo.Biz_ID
      if (td.edit_id > -1) {
         param.Category_ID = td.goodsCateList[td.edit_id].Category_ID
      }
      if (co.empty(param.Category_Name)) {
         wx.showToast({
            title: '分类名不能为空',
            icon: 'none'
         })
         return
      }
      // console.log('【获取param】===', param)
      co.getMealApi('jyhs/Prdmg/saveBizCategory', param, function (res) {
         // console.log('【输出添加桌子返回值】===', res.data)
         if (res.code == 0) {
            if (td.edit_id == -1) {
               _this.addButtonChange()
            } else {
               _this.setData({
                  edit_id: -1
               })
            }
            app.Data.goodsCateList = res.data
            _this.setData({
               isShowInputLabelArr: [0, 0],
               goodsCateList: res.data
            })
         }
      })
   },
   // 删除商品自定义分类
   delConfirm: function (e) {
         co.confirm('', '确认删除吗？', '取消', '删除', function (res) {
            if (res) {
               _this.delGoods(e)
            }
         })
   },
   delGoods: function (e) {
      var i = e.currentTarget.id
      var list = _this.data.goodsCateList
      co.getMealApi('jyhs/Prdmg/delBizCategory', {
         Biz_ID: appData.shopInfo.Biz_ID,
         Category_ID: list[i].Category_ID
      }, function (res) {
         if (res.code == 0) {
            console.log("【获取最后一次删除返回数据】===", res.data)
            _this.setData({
               goodsCateList: res.data
            })
            if (res.data.length == 0) {
               app.Data.goodsCateList = 0
            } else {
               app.Data.goodsCateList = res.data
            }
         } else {
            co.modal(res.msg)
         }
      })
   },
   // 获取商家自定义分类 
   getGoodsCateData: function () {
      co.getMealApi('jyhs/Prdmg/getBizCategoryList', {
         Biz_ID: appData.shopInfo.Biz_ID
      }, function (goodsData) {
         if (goodsData.code == 0) {
            console.log("【获取分类商品】===", goodsData.data)
            _this.setData({
               goodsCateList: goodsData.data
            })
         }
      })
   }
})